﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using System.Reflection;

namespace BITS.Copy
{
    public class ContextMenu
    {
        private const string FolderMenu = "Folder\\shell\\BITSCopyMenu";
        private const string FolderMenuCommand = "Folder\\shell\\BITSCopyMenu\\command";

        private const string FileMenu = "*\\shell\\BITSCopyMenu";
        private const string FileMenuCommand = "*\\shell\\BITSCopyMenu\\command";

        private const string MenuName = "BITSCopy";
        private const string Command = @"{1} -copy ""%1"" ""{0}""";

        public static void Register(string destination)
        {
            string cmd = Assembly.GetExecutingAssembly().Location;
            RegistryKey regmenu = null;
            RegistryKey regcmd = null;
            try
            {
                //Folder
                regmenu = Registry.ClassesRoot.CreateSubKey(FolderMenu);
                if (regmenu != null)
                    regmenu.SetValue("", MenuName);
                regcmd = Registry.ClassesRoot.CreateSubKey(FolderMenuCommand);
                if (regcmd != null)
                    regcmd.SetValue("", string.Format(Command, destination, cmd));


                //File
                regmenu = Registry.ClassesRoot.CreateSubKey(FileMenu);
                if (regmenu != null)
                    regmenu.SetValue("", MenuName);
                regcmd = Registry.ClassesRoot.CreateSubKey(FileMenuCommand);
                if (regcmd != null)
                    regcmd.SetValue("", string.Format(Command, destination, cmd));
            }
            finally
            {
                if (regmenu != null)
                    regmenu.Close();
                if (regcmd != null)
                    regcmd.Close();
            }
        }


        public static void UnRegister()
        {            
            Registry.ClassesRoot.DeleteSubKeyTree(FolderMenu, false);
            Registry.ClassesRoot.DeleteSubKeyTree(FileMenu, false);
        }
    }
}
